﻿using System;
using CzfSDK;
using System.Text;

namespace Server
{
	public static class BagManager
	{
		public const int COIN_ID = 1; // 金币
		public const int GEM_ID = 2; // 钻石
		public const int DUST_ID = 3; // 妖能碎片

		// 技能ID范围
		public const int SKILL_ID_MIN = 10001;
		public const int SKILL_ID_MAX = 99999;
		// 角色ID范围
		public const int ROLE_ID_MIN = 100000;
		public const int ROLE_ID_MAX = 199999;



		/// <summary>
		/// 给玩家增加物品或资源
		/// </summary>
		/// <param name="prize">
		/// 	增加的物品或资源,格式为: 
		/// 	技能:ID|ID
		/// 	道具:ID,Count|ID,Count
		/// </param>
		/// <param name="userId">User identifier.</param>
		/// <param name="msg1">Msg1.</param>
		/// <param name="msg2">Msg2.</param>
		public static void AddItem(string prize, int userId, string msg1, string msg2 = "")
		{
//			CzfLog.Info ("prize:"+prize);
			prize = prize.Trim ();
			if (prize != null && prize != "") {
				// 先检测
				foreach (var item in prize.Split('|')) {
					if (item.Trim () != "") {
						int id = Convert.ToInt32(item.Trim ().Split (',') [0]);
						int count = Convert.ToInt32 (item.Trim ().Split (',') [1]);
						if (id == COIN_ID) {
							// 金币
							if(count <= 0)
								throw new Exception ("数量错误:" + count + ",prize=" + prize);
						} else if(id == GEM_ID) {
							// 钻石
							if(count <= 0)
								throw new Exception ("数量错误:" + count + ",prize=" + prize);
						} else if(id == DUST_ID) {
							// 碎片
							if(count <= 0)
								throw new Exception ("数量错误:" + count + ",prize=" + prize);
						} else if(id.Between(ROLE_ID_MIN, ROLE_ID_MAX)){
							// 角色
							RoleManager.TryAddRole(userId, id);
						} else if (id.Between (SKILL_ID_MIN, SKILL_ID_MAX)) {
							// 技能
							SkillManager.TryAddSkill (userId, id);
						}else {
							throw new Exception ("未定义的道具ID:" + id + ",prize=" + prize);
						}
					}
				}
				// 后发道具
				foreach (var item in prize.Split('|')) {
					if (item.Trim () != "") {
						int id = Convert.ToInt32(item.Trim ().Split (',') [0]);
						int afterCount = 0;
						int count = Convert.ToInt32 (item.Trim ().Split (',') [1]);
						if (id == COIN_ID) {
							// 金币
							UserManager.AddCoin(userId, count);
						} else if(id == GEM_ID) {
							// 钻石
							UserManager.AddGem(userId, count);
						} else if(id == DUST_ID) {
							// 碎片
							UserManager.AddDust(userId, count);
						} else if(id.Between(ROLE_ID_MIN, ROLE_ID_MAX)){
							// 角色
							for(int i=0;i<count;i++)
								RoleManager.AddRole(userId, id);
							afterCount = 1;
						} else if (id.Between (SKILL_ID_MIN, SKILL_ID_MAX)) {
							// 技能
							for(int i=0;i<count;i++)
								SkillManager.AddSkill (userId, id);
							afterCount = 1;
						}
						// 记录日志
						LogManager.AddItemLog (userId, id, count, afterCount, msg1, msg2);
					}
				}

			}


		}

		/// <summary>
		/// 消耗玩家的资源
		/// </summary>
		/// <param name="prize">Prize.</param>
		/// <param name="userId">User identifier.</param>
		/// <param name="msg1">Msg1.</param>
		/// <param name="msg2">Msg2.</param>
		public static void DelItem(string prize, int userId, string msg1, string msg2 = "")
		{
			prize = prize.Trim ();
			if (prize != null && prize != "") {
				// 先检测
				foreach (var item in prize.Split('|')) {
					if (item.Trim () != "") {
						int id = Convert.ToInt32 (item.Trim ().Split (',') [0]);
						int count = Convert.ToInt32 (item.Trim ().Split (',') [1]);
						if (id == COIN_ID) {
							UserManager.TryAddCoin (userId, -count);
						} else if (id == GEM_ID) {
							UserManager.TryAddGem (userId, -count);
						} else if (id == DUST_ID) {
							UserManager.TryAddDust (userId, -count);
						} else {
							throw new Exception ("未定义的道具ID:" + id + ",prize=" + prize);
						}
					}
				}

				// 后发道具
				foreach (var item in prize.Split('|')) {
					if (item.Trim () != "") {
						int id = Convert.ToInt32(item.Trim ().Split (',') [0]);
						int afterCount = 0;
						int count = Convert.ToInt32 (item.Trim ().Split (',') [1]);
						if (id == COIN_ID) {
							afterCount = UserManager.AddCoin (userId, -count);
						} else if (id == GEM_ID) {
							afterCount = UserManager.AddGem (userId, -count);
						} else if (id == DUST_ID) {
							afterCount = UserManager.AddDust (userId, -count);
						}
						// 记录日志
						LogManager.AddItemLog (userId, id, -count, afterCount, msg1, msg2);
					}
				}
			}
		}
	}
}

